﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/12 13:29:58
*
* */
namespace Novel.Sys.Dal
{
    public class novel_search
    {
        public bool Exists(string keyword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_search]");
            strSql.Append(" where keyword=@keyword");
            SqlParameter[] parameters = {
                    new SqlParameter("@keyword", SqlDbType.NVarChar,50)};
            parameters[0].Value = keyword;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }
        public int AddKey(string Key)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [novel_search] ([keyword],[count]) VALUES (");
            strSql.Append("@keyword,@count");
            strSql.Append(");select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@keyword", SqlDbType.NVarChar),
                    new SqlParameter("@count", SqlDbType.Int)};
            parameters[0].Value = Key;
            parameters[1].Value = 1;
            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int GetCount(string Key)
        {
            int count = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_search] where keyword=@keyword");
            SqlParameter[] parameters = {
                    new SqlParameter("@keyword", SqlDbType.NVarChar,50)};
            parameters[0].Value = Key;
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["count"] != null && ds.Tables[0].Rows[0]["count"].ToString() != "")
                {
                    count = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
                }
            }
            return count;
        }

        public int UpdateCount(string Key,int count)
        {
            string sql = "UPDATE [novel_search] SET [count]='" + count + "' where [keyword]= '" + Key + "'";
          return  DbHelpers.NonQuery(sql);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strWhere = " Where " + strWhere;
            }

            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                orderby = " Order By " + orderby;
            }

            //startIndex = startIndex - 1;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM [novel_search] Where ID Not IN ");
            strSql.Append("(Select Top " + startIndex + " ID From [novel_search]" + strWhere + orderby + ")");
            strSql.Append(" And ID In ");
            strSql.Append("(Select Top " + endIndex + " ID From [novel_search]" + strWhere + orderby + ")");
            strSql.Append(orderby);
            return DbHelpers.GetDataSet(strSql.ToString());
        }

    }
}
